package com.example.experiment_2java.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.experiment_2java.entity.Book;
import com.example.experiment_2java.persistence.BookMapper;
import com.example.experiment_2java.util.result.DataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookMapper bookMapper;
    /**
     * 查询书籍
     *
     * @param bookName 书名
     * @param isbn 编号
     * @param author 作者
     * @param press 出版社
     * @param className 类别
     * #return DataResult
     */
    @GetMapping
    public DataResult searchBook(@RequestParam(value = "bookName",required = false) String bookName,
                     @RequestParam(value = "isbn", required = false) String isbn,
                     @RequestParam(value = "author", required = false) String author,
                     @RequestParam(value = "press", required = false) String press,
                     @RequestParam(value = "className", required = false) String className) {
        // 构造查询条件
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        if (bookName != null && !bookName.isEmpty()) {
            queryWrapper.like("book_name", bookName);
        }
        if (isbn != null && !isbn.isEmpty()) {
            queryWrapper.eq("isbn", isbn);
        }
        if (author != null && !author.isEmpty()) {
            queryWrapper.like("author", author);
        }
        if (press != null && !press.isEmpty()) {
            queryWrapper.like("press", press);
        }
        if (className != null && !className.isEmpty()) {
            queryWrapper.like("class_name", className);
        }

        // 查询书籍
        List<Book> books = bookMapper.selectList(queryWrapper);

        // 如果查询结果为空，返回空字符串
        if (books == null || books.isEmpty()) {
            return DataResult.succ();
        }
        DataResult dataResult = DataResult.successByDataArray(books);
        dataResult.setApiLink("/api");
        return dataResult;
    }

    /**
     * 新增书籍
     *
     * @param book
     * @return DataResult
     */
    @PostMapping
    public DataResult addBook(@RequestBody Book book) {
        if (bookMapper.insert(book) > 0) {
            return DataResult.successByMessage("新增书籍成功", null).setApiLink("/api");
        } else {
            return DataResult.err().setApiLink("/api");
        }
    }

    /**
     * 更新书籍
     *
     * @param book 书籍信息
     * @return DataResult
     */
    @PutMapping
    public DataResult updateBook(@RequestBody Book book) {
        if (book.getIsbn() == null) {
            return DataResult.err().setApiLink("/api");
        }
        if (bookMapper.updateById(book) > 0) {
            return DataResult.successByMessage("更新书籍成功", null).setApiLink("/api");
        } else {
            return DataResult.err().setApiLink("/api");
        }
    }

    /**
     * 删除书籍
     *
     * @param isbn 书籍 isbn
     * @return DataResult
     */
    @DeleteMapping
    public DataResult deleteBook(@RequestParam String isbn) {
        if (bookMapper.deleteById(isbn) > 0) {
            return DataResult.successByMessage("删除书籍成功", null).setApiLink("/api");
        } else {
            return DataResult.err().setApiLink("/api");
        }
    }
}
